import 'package:flutter/services.dart';
import 'package:flutter_elf/config/component_index.dart';
import 'dart:ui' as ui;
import 'dart:math';

import 'package:path_drawing/path_drawing.dart';


class SunAnimationWidget extends StatefulWidget{
  final double percent;

  SunAnimationWidget(this.percent);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return SunAnimationWidgetState();
  }
}

class SunAnimationWidgetState extends State<SunAnimationWidget> with TickerProviderStateMixin{
  AnimationController animationController;
  double percentage = 0.0;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    animationController = AnimationController(duration: Duration(seconds: 2),vsync: this,)..addListener((){
      percentage = animationController.value * widget.percent;
    });
    animationController.animateTo(1,curve: Curves.decelerate,duration: Duration(milliseconds: 600));
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Column(
        children: <Widget>[
          SizedBox(height: 100,),
          AnimatedBuilder(
            animation: animationController,
            builder:(context,child){
              return FutureBuilder<ui.Image>(
                future: load(R.staticImageSunPng),
                builder:(context,snap){
                  if(!snap.hasData)return Container();
                  return CustomPaint(
                    size:Size(200, 200) ,
                    painter: SunAnimationPaint(200, 200,Colors.red,Colors.black,'startTime','endTime',snap.data,percentage),
                  );
                } ,
              );
            } ,
          ),
        ],
      ),
    );
  }
  Future<ui.Image> load(String asset) async {
    ByteData data = await rootBundle.load(asset);
    ui.Codec codec = await ui.instantiateImageCodec(data.buffer.asUint8List(),targetWidth: 40,targetHeight: 40);
    ui.FrameInfo fi = await codec.getNextFrame();
    return fi.image;
  }
}

class SunAnimationPaint extends CustomPainter{

  double width;
  double height;
  Color sunColor;
  Color dashColor;
  String startTime;
  String endTime;
  double percent ;
  ui.Image image;
  SunAnimationPaint(this.width, this.height, this.sunColor, this.dashColor,
      this.startTime, this.endTime,this.image,this.percent);

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
        ..color = Colors.grey
        ..strokeWidth = 2
        ..style = PaintingStyle.stroke
        ..isAntiAlias = true;
    double centerH = height;
    double centerW = width / 2;
    Rect rect = Rect.fromCenter(center: Offset(centerW, centerH),width: size.width,height: size.height);
//    canvas.drawArc(rect, pi, pi, false, paint);
//    drawSun(canvas);

    double R = width / 2;
    double radians = pi * (1-percent) ;
//    paint.color = Colors.red;
    var startPoint = Offset(0,height);

    var path = Path();
    path.addArc(rect, pi, pi);
//    canvas.drawPath(path, paint);
    canvas.drawPath(
      dashPath(
        path,
        dashArray: CircularIntervalList<double>(<double>[5, 5]),
      ),
      paint,
    );

    var path2 = Path();
    paint.color = Colors.yellow;
    path2.moveTo(startPoint.dx, startPoint.dy);
    path2.addArc(rect, pi, pi - radians);
//    canvas.drawPath(path, paint);
    canvas.drawPath(
      dashPath(
        path2,
        dashArray: CircularIntervalList<double>(<double>[5, 5]),
      ),
      paint,
    );

    drawSun(canvas);
  }

  drawSun(Canvas canvas){
    Paint paint = Paint()
      ..color = Colors.black
      ..strokeWidth = 10
      ..strokeCap = StrokeCap.butt
      ..style = PaintingStyle.stroke
      ..isAntiAlias = true;
    double R = width / 2;
    double radians = pi * (1-percent) ;
    canvas.translate(width/2, height);
//    canvas.drawCircle(Offset(width/2*sin(pi * percent), width/2 * cos(pi * percent)), 20, paint);
//    canvas.drawCircle(Offset(height - R * cos(radius),100), 10, paint);
//    canvas.drawCircle(Offset(R * cos(radians),-R * sin(radians)), 5, paint);
    canvas.drawImage(image, Offset(R * cos(radians) - 20 ,-R * sin(radians)-20), paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return true;
  }
}